//
//  ChatHistroyModule.m
//  Gabber
//
//  Created by 惠国 陆 on 11-9-28.
//  Copyright 2011年 恒生聚源. All rights reserved.
//

#import "MessageModule.h"
#import "XMPPClient.h"
@implementation MessageModule

@synthesize messageStorage = _messageStorage;

@synthesize receiveMessageSound = _receiveMessageSound;
@synthesize sendMessageSound = _sendMessageSound;

- (id)init
{
	// This will cause a crash - it's designed to.
	// Only the init methods listed in XMPPvCardTempModule.h are supported.
	
	return [self initWithMessageStorage:nil dispatchQueue:NULL];
}

- (id)initWithDispatchQueue:(dispatch_queue_t)queue
{
	// This will cause a crash - it's designed to.
	// Only the init methods listed in XMPPvCardTempModule.h are supported.
	
	return [self initWithMessageStorage:nil dispatchQueue:NULL];
}

- (id)initWithMessageStorage:(id <MessageStorage>)storage
{
	return [self initWithMessageStorage:storage dispatchQueue:NULL];
}

- (id)initWithMessageStorage:(id <MessageStorage>)storage dispatchQueue:(dispatch_queue_t)queue
{
	NSParameterAssert(storage != nil);
    NSURL *msgSound   = [[NSBundle mainBundle] URLForResource: @"msg"
                                                withExtension: @"caf"];
    
    CFURLRef baseURL = (CFURLRef)msgSound;
    // Create a system sound object representing the sound file.
    AudioServicesCreateSystemSoundID (
                                      baseURL,
                                      &_receiveMessageSound
                                      );
    AudioServicesCreateSystemSoundID (
                                      baseURL,
                                      &_sendMessageSound
                                      );
	
	if ((self = [super initWithDispatchQueue:queue]))
	{
        if ([storage configureWithParent:self queue:moduleQueue])
		{
			_messageStorage = [storage retain];
		}
		else
		{
			DDLogError(@"%@: %@ - Unable to configure storage!", THIS_FILE, THIS_METHOD);
		}
	}
	return self;
}

- (BOOL)activate:(XMPPStream *)aXmppStream
{
	if ([super activate:aXmppStream])
	{
		// Custom code goes here (if needed)
		
		return YES;
	}
	
	return NO;
}

- (void)deactivate
{
	// Custom code goes here (if needed)
	
	[super deactivate];
}

- (void)dealloc
{
	[_messageStorage release];
	_messageStorage = nil;
    AudioServicesDisposeSystemSoundID (_sendMessageSound);
	AudioServicesDisposeSystemSoundID (_receiveMessageSound);

	[super dealloc];
}




- (void)xmppStream:(XMPPStream *)sender didSendMessage:(XMPPMessage *)message {
    DDLogVerbose(@"%@: %@ - %@", THIS_FILE, THIS_METHOD, message);
    if(message.isChatMessageWithBody){

        [_messageStorage handleMessage:message xmppStream:sender];
        if([[NSUserDefaults standardUserDefaults] boolForKey: kPlayMessageSentSound]){
            AudioServicesPlaySystemSound (_sendMessageSound);
        }
    }

}

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {
    DDLogVerbose(@"%@: %@ - %@", THIS_FILE, THIS_METHOD, message);
    if ([message isChatMessageWithBody])
	{
        [_messageStorage handleMessage:message xmppStream:sender];
        if([[NSUserDefaults standardUserDefaults] boolForKey: kPlayMessageReceivedSound]){
            AudioServicesPlaySystemSound (_receiveMessageSound);
        }
        
	}

}
@end
